package com.jarchie.androiddev.ui.activities

import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction
import com.google.android.material.navigation.NavigationBarView
import com.jarchie.androiddev.R
import com.jarchie.androiddev.databinding.ActivityMainBinding
import com.jarchie.androiddev.ui.fragments.ChannelFragment
import com.jarchie.androiddev.ui.fragments.FindFragment
import com.jarchie.androiddev.ui.fragments.HomeFragment
import com.jarchie.androiddev.ui.fragments.MineFragment
import com.jarchie.androiddev.ui.fragments.PublishFragment

class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener {
    private lateinit var binding: ActivityMainBinding
    private var homeFragment: HomeFragment? = null
    private var channelFragment: ChannelFragment? = null
    private var publishFragment: PublishFragment? = null
    private var findFragment: FindFragment? = null
    private var mineFragment: MineFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initData()
    }

    // 简单初始化逻辑
    fun initData() {
        // 绑定Tab项的事件监听
        binding.mNavBottom.setOnItemSelectedListener(this)
        /**
         * 处理图标和文本选中状态的颜色，可以使用如下方法在代码中设置，也可以使用在xml中属性配置的方式
         * val states = arrayOfNulls<IntArray>(2)
         * states[0] = IntArray(1) { android.R.attr.state_selected }
         * states[1] = intArrayOf()
         * val colors = intArrayOf(getColor(R.color.primary), getColor(R.color.color_666))
         * val colorStateList = ColorStateList(states, colors)
         * binding.mNavBottom.itemTextColor = colorStateList
         * binding.mNavBottom.itemIconTintList = colorStateList
         */
        // 初始化加载的Fragment
        setSelect(0)
    }

    // 底部Tab项的选中监听事件
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.home -> {
                setSelect(0)
                return true
            }

            R.id.channel -> {
                setSelect(1)
                return true
            }

            R.id.publish -> {
                setSelect(2)
                return true
            }

            R.id.find -> {
                setSelect(3)
                return true
            }

            R.id.mine -> {
                setSelect(4)
                return true
            }
        }
        return false
    }

    // Fragment的选中逻辑
    fun setSelect(pos: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragment(transaction)
        when (pos) {
            0 -> {
                if (homeFragment == null) {
                    homeFragment = HomeFragment()
                    transaction.add(R.id.contentView, homeFragment!!)
                } else {
                    transaction.show(homeFragment!!)
                }
            }

            1 -> {
                if (channelFragment == null) {
                    channelFragment = ChannelFragment()
                    transaction.add(R.id.contentView, channelFragment!!)
                } else {
                    transaction.show(channelFragment!!)
                }
            }

            2 -> {
                if (publishFragment == null) {
                    publishFragment = PublishFragment()
                    transaction.add(R.id.contentView, publishFragment!!)
                } else {
                    transaction.show(publishFragment!!)
                }
            }

            3 -> {
                if (findFragment == null) {
                    findFragment = FindFragment()
                    transaction.add(R.id.contentView, findFragment!!)
                } else {
                    transaction.show(findFragment!!)
                }
            }

            4 -> {
                if (mineFragment == null) {
                    mineFragment = MineFragment()
                    transaction.add(R.id.contentView, mineFragment!!)
                } else {
                    transaction.show(mineFragment!!)
                }
            }
        }
        transaction.commitNowAllowingStateLoss()
    }

    // 当Fragment不为空的时候先隐藏
    fun hideFragment(transaction: FragmentTransaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment!!)
        }
        if (channelFragment != null) {
            transaction.hide(channelFragment!!)
        }
        if (publishFragment != null) {
            transaction.hide(publishFragment!!)
        }
        if (findFragment != null) {
            transaction.hide(findFragment!!)
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment!!)
        }
    }
}